package net.dreamlu.common;

import javax.servlet.http.HttpServletRequest;

import net.dreamlu.qa.controller.QController;
import net.dreamlu.qa.controller.QuestionController;
import net.dreamlu.qa.controller.TagsController;
import net.dreamlu.qa.controller.UController;
import net.dreamlu.qa.controller.UserController;
import net.dreamlu.qa.interceptor.CookieLoginInterceptor;
import net.dreamlu.qa.model.Admin;
import net.dreamlu.qa.model.Answer;
import net.dreamlu.qa.model.MailVerify;
import net.dreamlu.qa.model.Question;
import net.dreamlu.qa.model.QuestionSort;
import net.dreamlu.qa.model.QuestionTag;
import net.dreamlu.qa.model.Tags;
import net.dreamlu.qa.model.User;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.RenderingTimeHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.ext.plugin.quartz.QuartzPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;

public class Config extends JFinalConfig {
    
    /**
     * 配置常量
     */
    public void configConstant(Constants me) {
        loadPropertyFile("db_config.txt"); // 加载少量必要配置，随后可用getProperty(...)获取值
        me.setDevMode(getPropertyToBoolean("devMode", true));
        me.setViewType(ViewType.JSP); // 设置视图类型为Jsp，否则默认为FreeMarker
    }
    
    /**
     * 配置路由
     */
    public void configRoute(Routes me) {
        me.add("/", CommonController.class);
        me.add("/u", UController.class);
        me.add("/q", QController.class);
        me.add("/t", TagsController.class);
        me.add("/user", UserController.class);
        me.add("/question", QuestionController.class);
    }
    
    /**
     * 配置插件
     */
    public void configPlugin(Plugins me) {
        // 数据库连接池插件
        DruidPlugin dp = new DruidPlugin(getProperty("jdbcUrl"),
                getProperty("user"), getProperty("password"));
        dp.addFilter(new StatFilter());
        WallFilter wall = new WallFilter();
        wall.setDbType("mysql");
        dp.addFilter(wall);
        me.add(dp);
        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp).setShowSql(true);
        me.add(arp);
        arp.addMapping("user_info", User.class);
        arp.addMapping("answer", Answer.class);
        arp.addMapping("question", Question.class);
        arp.addMapping("question_sort", QuestionSort.class);
        arp.addMapping("question_tag", QuestionTag.class);
        arp.addMapping("tags", Tags.class);
        arp.addMapping("admin", Admin.class);
        arp.addMapping("mail_verify", MailVerify.class);
        
        // 添加EhCache
        me.add(new EhCachePlugin());
        // 添加定时器
        me.add(new QuartzPlugin());
    }
    
    /**
     * 配置全局拦截器
     */
    public void configInterceptor(Interceptors me) {
        // cookie 记住密码
        me.add(new CookieLoginInterceptor());
        me.add(new SessionInViewInterceptor());
    }
    
    /**
     * 配置处理器
     */
    public void configHandler(Handlers me) {
        me.add(new RenderingTimeHandler());
        // Druid监控
        DruidStatViewHandler druidViewHandler = new DruidStatViewHandler(
                "/admin/druid", new IDruidStatViewAuth() {
                    public boolean isPermitted(HttpServletRequest request) {
                        return true;
                    }
                });
        me.add(druidViewHandler);
    }
    
    /**
     * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
     */
    public static void main(String[] args) {
        JFinal.start("WebRoot", 80, "/", 5);
    }
}
